import { Body, Controller, Get, Post, Res, HttpStatus, UploadedFile, UseInterceptors, HttpException, Query, Request } from '@nestjs/common';
import { AnswerReportErrors } from './answerReportErrors.entity';
import { AnswerReportErrorsService } from './answerReportErrors.service';
import {
  apibefore
} from '../../router'
import { ValidRSAInterceptor } from 'src/interceptor/validRSA';
import { ValidTokenInterceptor } from 'src/interceptor/validToken';
import utils from 'src/common/utils';
import { ReportSourceEnum } from './answerReportErrors.enum';
import { ValidUniqueLoginInterceptor } from 'src/interceptor/validUniqueLogin';
@Controller(apibefore('/answer_report_errors'))
export class AnswerReportErrorsController {
  constructor(private readonly service: AnswerReportErrorsService) {}

  @Post('create')
  @UseInterceptors(ValidRSAInterceptor)
  @UseInterceptors(ValidTokenInterceptor)
  async create(@Body() data: AnswerReportErrors,@Request() req){
    let wx_user = req.headers.wx_user
    let findData = await this.service.findByWxUser(wx_user, data.answer_id, data.type)
    if(findData) return true
    let addData = new AnswerReportErrors()
    addData.wx_user = wx_user
    addData.answer_id = data.answer_id
    addData.create_time = new Date()
    addData.type = data.type
    addData.platform = data.platform
    addData.account = data.account
    addData.ic_name = data.ic_name
    addData.avatar_id = data.avatar_id
    addData.avatar_img = data.avatar_img
    addData.img = data.img
    addData.img_id = data.img_id
    addData.source = ReportSourceEnum.report
    return await this.service.create(addData)
  }
  
  
  @Get('findAll_auth')
  @UseInterceptors(ValidUniqueLoginInterceptor)
  async findAll_auth(@Query() params, @Request() req){
    let {skip, take} = utils.getLimit(params.pageSize, params.pageIndex)
    return await this.service.findAll(skip, take)
  }

  
  @Get('findAll_notauth')
  async findAll_notauth(@Query() params, @Request() req){
    let {skip, take} = utils.getLimit(params.pageSize, params.pageIndex)
    let result = await this.service.findAll_manager(skip, take, params.video_id)
    result.dataList.filter((e:any) => e.video_url || e.video_url_source).map((e:any) => {
      if(e.video_url_source){
        e.video_url = e.video_url_source
      }
      utils.getPlayVideoUrl(e)
    })
    return result
  }


  @Get('findAll')
  @UseInterceptors(ValidRSAInterceptor)
  @UseInterceptors(ValidTokenInterceptor)
  async findAll(@Query() params, @Request() req){
    let {skip, take} = utils.getLimit(params.pageSize, params.pageIndex)
    return await this.service.findAll(skip, take)
  }

  @Post('checkReject_auth')
  @UseInterceptors(ValidUniqueLoginInterceptor)
  async checkReject_auth(@Body() data){
    return (await this.service.checkReject(data.id)).affected > 0
  }

  @Post('checkReject')
  @UseInterceptors(ValidRSAInterceptor)
  @UseInterceptors(ValidTokenInterceptor)
  async checkReject(@Body() data){
    return (await this.service.checkReject(data.id)).affected > 0
  }

  
  @Post('checkSuccess_auth')
  @UseInterceptors(ValidUniqueLoginInterceptor)
  async checkSuccess_auth(@Body() data){
    let err = await this.service.checkSuccess(data.id)
    if(err){
      return new HttpException(err, HttpStatus.INTERNAL_SERVER_ERROR)
    }else{
      return true
    }
  }

  @Post('checkSuccess')
  @UseInterceptors(ValidRSAInterceptor)
  @UseInterceptors(ValidTokenInterceptor)
  async checkSuccess(@Body() data){
    let err = await this.service.checkSuccess(data.id)
    if(err){
      return new HttpException(err, HttpStatus.INTERNAL_SERVER_ERROR)
    }else{
      return true
    }
  }

  
  @Post('deleteTag')
  @UseInterceptors(ValidUniqueLoginInterceptor)
  deleteTag(@Body() data){
    return this.service.deleteTag(data.id)
  }

}
